import pygame
from pygame.sprite import DirtySprite, LayeredUpdates
from animation import Animation
from level_resources import LevelResources
import copy

INF = 10000

class Obstacle(DirtySprite):
	def __init__(self, screen, image, x, y, obType):
		DirtySprite.__init__(self)

		self.screen = screen
		self.image = image
		self.baseImage = image
		self.rect = image.get_rect()
		self.rect.x = x
		self.rect.y = y
		self.obType = obType

		if self.obType == 1:
			self.life = 1
			self.collidable = False
		elif self.obType == 2:
			self.life = 1
			self.collidable = True
		elif self.obType == 3:
			self.life = 2
			self.collidable = True
		elif self.obType == 4:
			self.life = 3
			self.collidable = True
		elif self.obType == 5:
			self.life = INF
			self.collidable = True

		self.group = LayeredUpdates()
		if self.obType == 1:
			self.explosion = Animation(self.screen, self.rect, LevelResources.burnImages, LevelResources.burnDelays, False, 0)
		else:
			self.explosion = Animation(self.screen, self.rect, LevelResources.expImages, LevelResources.expDelays, False, 0)
		self.destroyed = False

	def hit(self):
		self.life -= 1
		if self.life == 0:
			self.destroy()

	def destroy(self):
		self.group.add(self.explosion)
		self.explosion.startAnimation()
	
	def update(self):
		if self.explosion.running:
			self.image = self.baseImage.copy()
			self.image.blit(self.explosion.image, (0, 0))
			self.group.update()

			if self.explosion.finished:
				self.kill()	#Delete obstacle
				self.destroyed = True